﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DBBase;
using System.IO;
using System.Xml;

namespace Business
{
    public partial class RolePermissions : Form
    {
        public MainForm m_mainform;
        public string[] cquanxian;
        private CheckBox[] cche;


        public string sname1;
        public string sname2;
        public string sname3;

        public string sname;

        public TreeNode nd1;
        public TreeNode nd2;
        public TreeNode nd3;
        public RolePermissions()
        {
            InitializeComponent();
            sname = "";
            treeView11.ExpandAll(); // 展开全部
            treeView12.ExpandAll(); // 展开全部
            treeView13.ExpandAll();  //展开全部
            treeView14.ExpandAll();  //展开全部

            treeView11.Visible = false; // 
            treeView12.Visible = false; // 
            treeView13.Visible = false;  //
            treeView14.Visible = false; //

           
            cche = new System.Windows.Forms.CheckBox[20] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12, checkBox13, checkBox14, checkBox15, checkBox16, checkBox17, checkBox18, checkBox19, checkBox20 };
            inicbx(lstUser, "juesequanxian");
            for (int i = 0; i < 20; i++)
            {
                cche[i].Visible = false;
            }
        }
        void inicbx(ListView cbx, string dataname)
        {
            cbx.Items.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0} order by jsqx_jslx", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = dt.Rows[i]["jsqx_jsmc"].ToString().Trim();
                li.SubItems.Add(dt.Rows[i]["jsqx_jslx"].ToString().Trim());
                
                cbx.Items.Add(li);
            }
        }
        private void getallTreeNode(TreeNodeCollection nodes)
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                sname1 = td.Text;
                if (td.Nodes.Count > 0)
                    getallTreeNodezl2(td.Nodes);
            }
        }
        private void getallTreeNodezl2(TreeNodeCollection nodes)//子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                sname2 = td.Text;
                if (td.Nodes.Count > 0)
                {
                    getallTreeNodezl3(td.Nodes);
                }
                else
                {

                    if (td.Checked)
                    {
                        if (sname == "")
                        {
                            sname = td.Text;
                        }
                        else
                        {
                            sname = sname + "," + sname2;
                        }
                    }

                }

            }
        }
        private void getallTreeNodezl3(TreeNodeCollection nodes)//三级子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                sname3 = td.Text;
                bool bche = td.Checked;
                if (td.Nodes.Count > 0)
                {
                    getallTreeNodezl3(td.Nodes);
                }
                else
                {
                    if (td.Checked)
                    {
                        if (sname == "")
                        {
                            sname = td.Text;
                        }
                        else
                        {
                            sname = sname + "," + sname2 + sname3;
                        }
                    }
                }
            }
        }

        private void getsetTreeNode(TreeNodeCollection nodes)
        {

            foreach (TreeNode td in nodes)
            {
                td.Checked = false;
                nd1 = td;
                //  td.Text 读取该node;
                sname1 = td.Text;
                if (td.Nodes.Count > 0)
                    getsetTreeNode2(td.Nodes);
            }
        }

        private void getsetTreeNode2(TreeNodeCollection nodes)//子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                td.Checked = false;
                nd2 = td;
                sname2 = td.Text;
                if (td.Nodes.Count > 0)
                {
                    getsetTreeNode3(td.Nodes);
                }
                else
                {
                    if (getmoqx(sname2, text_jsmc.Text))
                    {
                        td.Checked = true;
                        nd1.Expand();
                    }
                    else
                    {
                        td.Checked = false;
                    }
                }

            }
        }
        private void getsetTreeNode3(TreeNodeCollection nodes)//三级子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                td.Checked = false;
                nd3 = td;
                sname3 = td.Text;
                bool bche = td.Checked;
                if (td.Nodes.Count > 0)
                {
                    getsetTreeNode3(td.Nodes);
                }
                else
                {
                    if (getmoqx(sname2 + sname3, text_jsmc.Text))
                    {
                        td.Checked = true;
                        nd2.Expand();
                    }
                    else
                    {
                        td.Checked = false;
                    }
                }
            }
        }
        public bool getmoqx(string name, string sleixing)
        {

            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"Select * From juesequanxian where jsqx_jsmc='{0}' and jsqx_qx like '%%{1}%%'",sleixing, name);
            if (objquery.selectDBcount(ssql)>0)
            {
                return true;
            }
            else
            {
                return false;
            }



        }



        private void lstUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lstUser.SelectedItems)
            {
              
                string test1 = item.SubItems[0].Text.ToString().Trim();
                string test2 = item.SubItems[1].Text.ToString().Trim();
                text_jsmc.Text = test1;
                com_jslx.Text = test2;
                if (test2.Contains("系统管理"))
                {
                    getsetTreeNode(treeView11.Nodes);
                }
                else if (test2.Contains("安全"))
                {
                    getsetTreeNode(treeView12.Nodes);
                }
                else if (test2.Contains("审计"))
                {
                    getsetTreeNode(treeView13.Nodes);
                }
                else if (test2.Contains("业务"))
                {
                    getsetTreeNode(treeView14.Nodes);
                }
                string sql = string.Format(@"Select * From juesequanxian where jsqx_jsmc='{0}' and jsqx_jslx='{1}' ", test1,test2);
                Query mquery = new Query();
                string squanxian = mquery.GetDBString(sql, "jsqx_qx");
                if (squanxian != "")
                {
                    cquanxian = squanxian.Split(new char[] { ',' });
                    if (cquanxian.Length > 0)
                    {
                        //for (int i = 0; i < cquanxian.Length; i++)
                        //{
                        //    for (int j = 0; j < 20; j++)
                        //    {
                        //        if (cquanxian[i].Contains(cche[j].Text))
                        //        {
                        //            cche[j].Checked = true;
                        //            break;
                        //        }
                        //    }
                        //}
                    }
                }
            }
        }

       

        private void com_jslx_SelectedIndexChanged(object sender, EventArgs e)
        {
            sname = "";
            if (com_jslx.Text.Contains("系统管理"))
            {
                treeView11.Visible = true; // 展开全部
                treeView12.Visible = false; // 展开全部
                treeView13.Visible = false;  //展开全部
                treeView14.Visible = false; //展开全部
            }
            else if (com_jslx.Text.Contains("安全管理"))
            {
                treeView11.Visible = false; // 展开全部
                treeView12.Visible = true; // 展开全部
                treeView13.Visible = false;  //展开全部
                treeView14.Visible = false; //展开全部

            } if (com_jslx.Text.Contains("审计管理"))
            {

                treeView11.Visible = false; // 展开全部
                treeView12.Visible = false; // 展开全部
                treeView13.Visible = true;  //展开全部
                treeView14.Visible = false; //展开全部
            }
            if (com_jslx.Text.Contains("业务"))
            {

                treeView11.Visible = false; // 展开全部
                treeView12.Visible = false; // 展开全部
                treeView13.Visible = false;  //展开全部
                treeView14.Visible = true; //展开全部
            }
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            if (com_jslx.Text.Contains("系统管理"))
            {
                getallTreeNode(treeView11.Nodes);
            }
            else if (com_jslx.Text.Contains("安全管理"))
            {
                getallTreeNode(treeView12.Nodes);
            }
            else if (com_jslx.Text.Contains("审计管理"))
            {
                getallTreeNode(treeView13.Nodes);
            }
            else if (com_jslx.Text.Contains("业务"))
            {
                getallTreeNode(treeView14.Nodes);
            }
            string sqx = sname;

            Query objquery = new Query();
            string ssql = "";

            ssql = string.Format(@"Select * From juesequanxian where jsqx_qx='{0}' ", sqx);
            if (objquery.selectDBcount(ssql) > 0)
            {
                MessageBox.Show("已经存在相同权限的角色名称请勿重复添加");
                return;
            }
            ssql = string.Format(@"Select * From juesequanxian where jsqx_jsmc='{0}' ", text_jsmc.Text);

            if (objquery.ExecuteS(ssql))
            {
                ssql = string.Format(@"update  juesequanxian set jsqx_qx='{0}' where jsqx_jsmc='{1}'", sqx, text_jsmc.Text);
                //保存操作日记
                m_mainform.savecaozuoriji(ssql, "更新", "系统管理", "角色功能新增");
            }
            else
            {
                ssql = string.Format(@"insert into juesequanxian (
                    jsqx_jsmc,
                    jsqx_jslx,
                    jsqx_qx) values 
                    ('{0}',
                    '{1}',
                    '{2}')", text_jsmc.Text, com_jslx.Text, sqx);
                //保存操作日记
                m_mainform.savecaozuoriji(ssql, "新增", "系统管理", "角色功能新增");

            }
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("角色管理增加"))
            {
                m_mainform.baocunhxrj("角色管理增加", string.Format("新增{0}角色管理 {1}", text_jsmc.Text, "insert into juesequanxian"), "写入");// 核心功能日记  日记内容
            }
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("角色保存失败");
            }
            else
            {
                MessageBox.Show("角色保存成功");
            }
          


            for (int i = 0; i < 20; i++)
            {
                cche[i].Checked = false;
            }
            inicbx(lstUser, "juesequanxian");
  
        }

        private void but_delete_Click(object sender, EventArgs e)
        {
            if (text_jsmc.Text != "")
            {
                Query objquery = new Query();
                string ssql = string.Format(@"delete juesequanxian where jsqx_jsmc='{0}' ", text_jsmc.Text);
                if (objquery.InsertDB(ssql))
                {
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
                //保存操作日记
                m_mainform.savecaozuoriji(ssql, "删除", "系统管理", "角色删除");
                //判断是否核心功能
                if (m_mainform.shxgn.Contains("角色管理删除"))
                {
                    m_mainform.baocunhxrj("角色管理删除", string.Format("删除{0}角色 {1}", text_jsmc.Text,ssql), "删除");// 核心功能日记  日记内容
                }
                for (int i = 0; i < 20; i++)
                {
                    cche[i].Visible = false;
                }
                inicbx(lstUser, "juesequanxian");
            }
        }

        private void RolePermissions_Load(object sender, EventArgs e)
        {
            //判断是否民警
            if (m_mainform.sfjy.Contains("角色管理增加"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_save.Enabled = false;
                }
            }

            if (m_mainform.sfjy.Contains("角色管理删除"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_delete.Enabled = false;
                }
            }


        }
    }
}
